package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.exception.UpdateFlagException;
import com.itany.mapper.BookMapper;
import com.itany.mapper.ExamineMapper;
import com.itany.pojo.Book;
import com.itany.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author：sl
 * Date：2019-09-21 下午5:35
 * Description：<描述>
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private ExamineMapper examineMapper;


    @Transactional(readOnly = true)
    public PageInfo<Book> findAllBook(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        List<Book> books = bookMapper.selectAll();
        PageInfo<Book> info = new PageInfo<Book>(books);
        return info;
    }


    public void modifyBookFlag(Book book) throws UpdateFlagException {
        //先查询该书是否为未审核状态
        Book rebook = examineMapper.selectBookById(book.getId());

        if (0==rebook.getFlag()){
            throw new UpdateFlagException("不能修改未审核书籍的状态，请先去审核");
        }
        bookMapper.updateBookFlagById(book);
    }
}
